Lecture 5 (Part 1) - Practice
Q1: Population Density
اعمل قاعدة بيانات للسكان والمساحة للدول دي:
| Country | Population (in million people) | Area (in millions of square miles) |
|---|---|---|
| Usa | 203 | 3 |
| India | 548 | 1 |
| China | 800 | 4 |
| Brazil | 108 | 3 |
وبعدين اعمل rule: ا density is population divided by area.
واكتب الاستعلامات:
- Find the population of India.
- Find the area of Brazil.
- Find the population density of China.
% Facts
pop(usa, 203).
pop(india, 548).
pop(china, 800).
pop(brazil, 108).
area(usa, 3).
area(india, 1).
area(china, 4).
area(brazil, 3).
% Rule
density(X, Y) :- pop(X, P), area(X, A), Y is P / A.
Queries:
?- pop(india, X). % X = 548
?- area(brazil, X). % X = 3
?- density(china, X). % X = 200
Q2: Arithmetic Functions
اعمل rules للدوال الرياضية دي:
- Sum: S = X + Y
- Calculate: y = (x + 3)²
- Polynomial: F = X⁴ + 2X + 5
واكتب الاستعلامات:
- Find the sum of 5 and 4.
- Find y when x = 2 using calculate.
- Find F when X = 3 using the polynomial.
% Rules
sum(X, Y, S) :- S is X + Y.
calculate(X, Y) :- Y is (X + 3) ** 2.
poly_func(X, F) :- F is X ** 4 + 2 * X + 5.
Queries:
?- sum(5, 4, S). % S = 9
?- calculate(2, Y). % Y = 25
?- poly_func(3, F). % F = 92 (81 + 6 + 5)
Q3: Factorial
اعمل recursive rule لحساب factorial:
- Base: factorial of 1 is 1.
- Inductive: factorial of N is N × factorial of N-1.
واكتب الاستعلام:
- Find the factorial of 5.
fac(1, 1).
fac(N, F) :- N > 1, M is N - 1, fac(M, Fm), F is N * Fm.
Queries:
?- fac(5, F). % F = 120
Q4: GCD
اعمل recursive rule لحساب greatest common divisor (GCD):
- If X and Y are equal, D = X.
- If X < Y, D = gcd(X, Y - X).
- If Y < X, D = gcd(Y, X).
واكتب الاستعلامات:
- Find the GCD of 12 and 8.
- Find the GCD of 100 and 35.
gcd(X, X, X).
gcd(X, Y, D) :- X < Y, Y1 is Y - X, gcd(X, Y1, D).
gcd(X, Y, D) :- Y < X, gcd(Y, X, D).
Queries:
?- gcd(12, 8, D). % D = 4
?- gcd(100, 35, D). % D = 5
Q5: Logic Gates
اعمل Facts للبوابات المنطقية الأساسية (not, and, or, xor):
| Inputs | and | or | xor |
|---|---|---|---|
| 0, 0 | 0 | 0 | 0 |
| 0, 1 | 0 | 1 | 1 |
| 1, 0 | 0 | 1 | 1 |
| 1, 1 | 1 | 1 | 0 |
| Inputs | not |
|---|---|
| 0 | 1 |
| 1 | 0 |
not(0, 1). not(1, 0).
and(0, 0, 0). and(0, 1, 0).
and(1, 0, 0). and(1, 1, 1).
or(0, 0, 0). or(0, 1, 1).
or(1, 0, 1). or(1, 1, 1).
xor(0, 0, 0). xor(0, 1, 1).
xor(1, 0, 1). xor(1, 1, 0).
Q6: Half Adder
استخدم "and" و "xor" من Q5 عشان تبني half-adder:
- Carry = A and B
- Sum = A xor B
وجرب inputs (0,1) و (1,1).
halfadder(A, B, Carry, Sum) :-
and(A, B, Carry),
xor(A, B, Sum).
Queries:
?- halfadder(0, 1, C, S). % C = 0, S = 1
?- halfadder(1, 1, C, S). % C = 1, S = 0
?- halfadder(0, 0, C, S). % C = 0, S = 0
?- halfadder(1, 0, C, S). % C = 0, S = 1
Q7: Boolean Function
استخدم "not", "and", "or" من Q5 عشان تعمل rule للدالة:
F = B' + AC
يعني: not B, then and(A, C), then or the results.
وجرب inputs:
- A=1, B=0, C=1
- A=1, B=1, C=1
- A=0, B=1, C=1
- A=0, B=0, C=0
bool_func(A, B, C, F) :-
not(B, B_not),
and(A, C, AC),
or(B_not, AC, F).
Queries:
?- bool_func(1, 0, 1, F). % F = 1
?- bool_func(1, 1, 1, F). % F = 1
?- bool_func(0, 1, 1, F). % F = 0
?- bool_func(0, 0, 0, F). % F = 1
Q8: Summation 1 to X
اعمل recursive rule تجمع الأرقام من 1 لأي رقم X:
F = 1 + 2 + 3 + ... + X
Base: sum_to(1, 1).
Recursive: sum_to(X, S) :- sum_to(X-1, S1), S is S1 + X.
واكتب الاستعلامات:
- Sum from 1 to 5.
- Sum from 1 to 10.
- Sum from 1 to 100.
sum_to(1, 1).
sum_to(X, S) :- X > 1, X1 is X - 1, sum_to(X1, S1), S is S1 + X.
Queries:
?- sum_to(5, S). % S = 15
?- sum_to(10, S). % S = 55
?- sum_to(100, S). % S = 5050
Q9: Summation of Odd Numbers 1 to M
اعمل recursive rule تجمع الأرقام الفردية (odd) من 1 لأي رقم فردي M:
- لو M فردي (M mod 2 =:= 1): ضيفه للمجموع
- لو M زوجي: تخطاه
Base: sum_odd(1, 1).
واكتب الاستعلامات:
- Sum of odd numbers from 1 to 5.
- Sum of odd numbers from 1 to 7.
- Sum of odd numbers from 1 to 10.
sum_odd(1, 1).
sum_odd(M, S) :-
M > 1,
M1 is M - 1,
sum_odd(M1, S1),
(M mod 2 =:= 1 -> S is S1 + M ; S is S1).
Queries:
?- sum_odd(5, S). % S = 9 (1+3+5)
?- sum_odd(7, S). % S = 16 (1+3+5+7)
?- sum_odd(10, S). % S = 25 (1+3+5+7+9)